package homework.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Comparator比较器接口对下列5位同学的成绩做降序排序，
 * 如果成绩相同，那在成绩排序的基础上按照年龄升序排序。
 */
public class CollectionsTest {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三",20,90.0f));
        list.add(new Student("李四",22,95.0f));
        list.add(new Student("王五",26,95.0f));
        list.add(new Student("赵六",22,100.0f));
        list.add(new Student("田七",26,87.0F));
        System.out.println(
                "-------------------前--------------------------------");

        System.out.println("\t姓名（String）\t年龄（int）\t分数（float）");
        list.forEach(System.out::println);


        System.out.println(
                "-------------------------后--------------------------------");
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student student, Student t1) {
                int result = (int) (t1.getScore() - student.getScore());
                if (result == 0){
                    return t1.getAge() - student.getAge();
                }
                return result;
            }
        });

        list.forEach(System.out::println);
    }


}
class Student{
    private String name;
    private int age;
    private float score;

    public Student() {
    }

    @Override
    public String toString() {
        return "\t" + name  + "\t\t\t" + age + "\t\t\t\t" + score;
    }

    public Student(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }
}
